# -*- coding: utf-8 -*-
from www.shortcuts import *
from datetime import datetime
from hashlib import md5
from cStringIO import StringIO
import random
a = 0


class Simple(Handler):
    rule = Rule("/simple/")
    def get(self):
        return Response("Hello Wold! %s %s"%(
            self.request.client_session.get("x", "nothing"),
            self.request.session.get('x', 'none')
        ))

    def post(self):
        a = int(self.request.form.get("a"))
        b = int(self.request.form.get("b"))
        self.request.client_session["x"] = a+b
        self.request.session["x"] = a-b
        return Response("结果为：%s"%(a+b,))


class Index(Handler):
    rule = Rule('/')
    @cache.cache_page(5)
    def get(self):
        global a
        a += 1
        tmpl = env.get_template("index.html")
        return Response(tmpl.render(name=random.random(), a=a))


class rpc1(XMLRPC):
    rule = Rule('/inner/xmlrpc/')
    def rpc_pow(self, a, b):
        return a ** b

    def rpc_add(self, a, b):
        return a + b


class rpc2(JSONRPC):
    rule = Rule('/inner/jsonrpc/')
    def rpc_pow(self, a, b):
        return a ** b

    def rpc_add(self, a, b):
        return a + b


class RPCBenchmark(Handler):
    rule = Rule('/benchmark/')
    def get(self):
        from jsonrpc import ServiceProxy
        from xmlrpclib import ServerProxy
        from phprpc import PHPRPC_Client
        j = ServiceProxy("http://127.0.0.1:7777/jsonrpc")
        x = ServerProxy("http://127.0.0.1:7777/xmlrpc")
        p = PHPRPC_Client("http://127.0.0.1:7777/phprpc").useService()
        xi = ServerProxy("http://127.0.0.1:7777/inner/xmlrpc/")
        ji = ServiceProxy("http://127.0.0.1:7777/inner/jsonrpc/")

        return Response("""<img src="%s" alt="rpc benckmark" />"""%url)


class TestPhprpc(Handler):
    rule = Rule("/test/phprpc/")
    def get(self):
        tmpl = env.get_template("phprpc.html")
        return Response(tmpl.render())


#class Captcha(CaptchaHandler):
#    rule = Rule("/captcha/")
